/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package StringParser;

import java.util.ArrayList;
import java.util.Date;
import model.Image;
import model.TrafficPoint;

/**
 *
 * @author RodriguesMoises
 */
public class StringToTrafficPoint {
    public static TrafficPoint parse(String string)
    {
        if(string.startsWith("|TrafficPoint{") && string.endsWith("}TrafficPoint|"))
        {
            TrafficPoint trafficPoint = new TrafficPoint();
            int indexOfPointId;
            indexOfPointId = string.indexOf("pointId=") + "pointId=".length();
            String pointId = string.substring(indexOfPointId, string.indexOf(",| latitude="));
            trafficPoint.setPointId(Integer.parseInt(pointId));
            
            int indexOfLatitude;
            indexOfLatitude = string.indexOf(",| latitude=") + ",| latitude=".length();
            String latitude = string.substring(indexOfLatitude, string.indexOf(",| longitude="));
            trafficPoint.setLatitude(Integer.parseInt(latitude));
            
            int indexOfLongitude;
            indexOfLongitude = string.indexOf(",| longitude=") + ",| longitude=".length();
            String longitude = string.substring(indexOfLongitude, string.indexOf(",| images="));
            trafficPoint.setLongitude(Integer.parseInt(longitude));
            
            int indexOfImages;
            indexOfImages = string.indexOf(",| images=") + ",| images=".length();
            String images = string.substring(indexOfImages, string.indexOf(",| users="));
            //trafficPoint.setImages(getImagesFromString(images));
            
            int indexOfUsers;
            indexOfUsers = string.indexOf(",| users=") + ",| users=".length();
            String users = string.substring(indexOfUsers, string.indexOf(",| descriptions="));
            trafficPoint.setUsers(getUsersFromString(users));
            
            int indexOfDescriptions;
            indexOfDescriptions = string.indexOf(",| descriptions=") + ",| descriptions=".length();
            String descriptions = string.substring(indexOfDescriptions, string.indexOf(",| time="));
            trafficPoint.setDescriptions(getDescriptionsFromString(descriptions));
            
            int indexOfTime;
            indexOfTime = string.indexOf(",| time=") + ",| time=".length();
            String time = string.substring(indexOfTime, string.indexOf("}TrafficPoint|"));
            trafficPoint.setTime(getTimeFromString(time));
            
            return trafficPoint;
        }
        return null;
    }
    
    private static Image[] getImagesFromString(String string)
    {
        String regex = "|Image{";
        String[] strings = string.split(regex);
        Image[] images = new Image[strings.length];
        for(int i = 0; i < strings.length; i++)
        {
            images[i] = StringToImage.parse(regex + strings[i]);
        }
        return images;
    }
    
    public static ArrayList<TrafficPoint> getTrafficPointsFromString(String string)
    {
        ArrayList<TrafficPoint> trafficPoints = null;
        if(string.startsWith("[") && string.endsWith("]"))
        {
            trafficPoints = new ArrayList<TrafficPoint>();
//            String regex = "TrafficPoint{";
//            String[] strings = string.split(regex);
//            for (int i = 1; i < strings.length; i++) {
//                trafficPoints.add(parse(regex + strings[i]));
//            }
            int i1 = 0;
            int i2 = -1;
            while(i2 < string.length())
            {
                i1 = string.indexOf("|TrafficPoint{", i1+1);
                i2 = string.indexOf("}TrafficPoint|", i2+1) + "}TrafficPoint|".length();
                if(i2 > 0 && i1 > 0)
                {
                    String string1 = string.substring(i1, i2);
                    //System.out.println("String:\n" + string1);
                    TrafficPoint trafficPoint = StringToTrafficPoint.parse(string1);
                    trafficPoints.add(trafficPoint);
                }
                else
                {
                    break;
                }
            }
        }
        return trafficPoints;
    }
    
    private static ArrayList<Long> getUsersFromString(String string)
    {
        ArrayList<Long> users = null;
        if(string.startsWith("[") && string.endsWith("]"))
        {
            String string1 = "";
            users = new ArrayList<Long>();
            int i1 = 0;
            int i2 = -1;
            while(i2 < string.length())
            {
                i1 = i2 + 1;
                i2 = string.indexOf("|,", i1+1);
                if(i2 > 0)
                {
                    string1 = string.substring(i1+((users.isEmpty())? 1 : 2), i2);
                    users.add(Long.parseLong(string1));
                }
                else
                {
                    break;
                }
            }
            string1 = string.substring(i1+((users.isEmpty())? 1 : 2), string.length()-1);
            users.add(Long.parseLong(string1));
//            String[] strings = string.substring(1, string.length()-1).split(regex);
//            for (String str : strings) {
//                users.add(Long.parseLong(str));
//            }
            
        }
        return users;
    }
    
    private static ArrayList<String> getDescriptionsFromString(String string)
    {
        ArrayList<String> strings = null;
        if(string.startsWith("[") && string.endsWith("]"))
        {
            String string1 = "";
            strings = new ArrayList<String>();
//            int i1 = 0;
//            int i2 = 0;
//            while(i2 < string.length())
//            {
//                i1 = i2;
//                i2 = string.indexOf(",", i1);
//                strings.add(string.substring(i1, i2));
//            }
            
            int i1 = 0;
            int i2 = -1;
            while(i2 < string.length())
            {
                i1 = i2 + 1;
                i2 = string.indexOf("|,", i1+1);
                if(i2 > 0)
                {
                    string1 = string.substring(i1+(strings.isEmpty()? 1 : 2), i2);
                    strings.add(string1);
                }
                else
                {
                    break;
                }
            }
            string1 = string.substring(i1+(strings.isEmpty()? 1 : 2), string.length()-1);
            strings.add(string1);
        }
        return strings;
    }
    
    private static Date getTimeFromString(String string)
    {
        Date date = null;
        if(!string.equals("null"))
        {
            int day = Integer.parseInt(string.substring(0, 2));
            int month = Integer.parseInt(string.substring(3, 5));
            int year = Integer.parseInt(string.substring(6, 10));
            int hour = Integer.parseInt(string.substring(11, 13));
            int min = Integer.parseInt(string.substring(14, 16));
            int sec = Integer.parseInt(string.substring(17, 19));
            date = new Date(year-1900, month - 1, day, hour, min, sec);
        }
        return date;
    }
}
